package com.xique.park.controller.web;

import com.xique.common.core.enums.PayType;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.domain.PageResult;
import com.xique.common.log.annotation.Log;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.park.bean.request.web.payorder.PayOrderRequest;
import com.xique.park.service.service.ExportService;
import com.xique.park.service.service.IPayOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;

/**
 * 支付记录 controller
 *
 * @author wld
 * @date 2022/3/11 13:08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/web/payOrder")
@Slf4j
public class WebPayOrderController {

    private final IPayOrderService payOrderService;

    private final ExportService exportService;

    @Log(title = "分页查询全部支付记录")
    @PreAuthorize(hasPermi = "parking:pay:list")
    @PostMapping(value = "/list")
    public AjaxResult list(@RequestBody(required = false) PayOrderRequest payOrderRequest) {
        payOrderRequest = checkParam(payOrderRequest);
        PageResult pageResult = payOrderService.selectPageList(payOrderRequest);
        return AjaxResult.success(pageResult);
    }

    @Log(title = "分页查询微信支付记录")
    @PreAuthorize(hasPermi = "parking:pay:list")
    @PostMapping(value = "/listByWx")
    public AjaxResult listByWx(@RequestBody(required = false) PayOrderRequest payOrderRequest) {
        payOrderRequest = checkParam(payOrderRequest);
        payOrderRequest.setPayType(Collections.singletonList(PayType.WX_PAY.getType()));
        PageResult pageResult = payOrderService.selectPageList(payOrderRequest);
        return AjaxResult.success(pageResult);
    }

    @Log(title = "分页查询支付宝支付记录")
    @PreAuthorize(hasPermi = "parking:pay:list")
    @PostMapping(value = "/listByAliPay")
    public AjaxResult listByAliPay(@RequestBody(required = false) PayOrderRequest payOrderRequest) {
        payOrderRequest = checkParam(payOrderRequest);
        payOrderRequest.setPayType(Collections.singletonList(PayType.ALI_PAY.getType()));
        PageResult pageResult = payOrderService.selectPageList(payOrderRequest);
        return AjaxResult.success(pageResult);
    }

    @Log(title = "分页查询ETC支付记录")
    @PreAuthorize(hasPermi = "parking:pay:list")
    @PostMapping(value = "/listByEtc")
    public AjaxResult listByEtc(@RequestBody(required = false) PayOrderRequest payOrderRequest) {
        payOrderRequest = checkParam(payOrderRequest);
        payOrderRequest.setPayType(Collections.singletonList(PayType.ETC.getType()));
        PageResult pageResult = payOrderService.selectPageList(payOrderRequest);
        return AjaxResult.success(pageResult);
    }

    @Log(title = "分页查询储值支付记录")
    @PreAuthorize(hasPermi = "parking:pay:list")
    @PostMapping(value = "/listBySavings")
    public AjaxResult listBySavings(@RequestBody(required = false) PayOrderRequest payOrderRequest) {
        payOrderRequest = checkParam(payOrderRequest);
        payOrderRequest.setPayType(Collections.singletonList(PayType.SAVINGS.getType()));
        PageResult pageResult = payOrderService.selectPageList(payOrderRequest);
        return AjaxResult.success(pageResult);
    }

    @Log(title = "分页查询现金支付记录")
    @PreAuthorize(hasPermi = "parking:pay:list")
    @PostMapping(value = "/listByCash")
    public AjaxResult listByCash(@RequestBody(required = false) PayOrderRequest payOrderRequest) {
        payOrderRequest = checkParam(payOrderRequest);
        payOrderRequest.setPayType(Collections.singletonList(PayType.CASH.getType()));
        PageResult pageResult = payOrderService.selectPageList(payOrderRequest);
        return AjaxResult.success(pageResult);
    }

    @Log(title = "分页查询其他支付记录")
    @PreAuthorize(hasPermi = "parking:pay:list")
    @PostMapping(value = "/listByOther")
    public AjaxResult listByOther(@RequestBody(required = false) PayOrderRequest payOrderRequest) {
        payOrderRequest = checkParam(payOrderRequest);
        payOrderRequest.setPayType(Arrays.asList(PayType.P_CLOUD.getType(), PayType.OTHER.getType()));
        PageResult pageResult = payOrderService.selectPageList(payOrderRequest);
        return AjaxResult.success(pageResult);
    }

    @Log(title = "分页查询数字人名币支付记录")
    @PreAuthorize(hasPermi = "parking:pay:list")
    @PostMapping(value = "/listByRmb")
    public AjaxResult listByRmb(@RequestBody(required = false) PayOrderRequest payOrderRequest) {
        payOrderRequest = checkParam(payOrderRequest);
        payOrderRequest.setPayType(Collections.singletonList(PayType.E_RMB.getType()));
        PageResult pageResult = payOrderService.selectPageList(payOrderRequest);
        return AjaxResult.success(pageResult);
    }

    @Log(title = "支付记录导出")
    @PreAuthorize(hasPermi = "parking:pay:export")
    @PostMapping(value = "/export")
    public void exportPayRecord(@RequestBody(required = false) PayOrderRequest payOrderRequest, HttpServletResponse response) throws IOException {
        payOrderRequest = checkParam(payOrderRequest);
        exportService.exportPayRecord(response, payOrderRequest);
    }

    private PayOrderRequest checkParam(PayOrderRequest request) {
        if (request == null) {
            request = new PayOrderRequest();
        }
        int pageNum = request.getPageNum() == null ? 1 : request.getPageNum();
        int pageSize = request.getPageSize() == null ? 10 : request.getPageSize();
        if (request.getParkId() == null) {
            Long parkId = SecurityUtils.getParkId();
            if (parkId != null) {
                request.setParkId(parkId);
            }
        }
        request.setPageNum(pageNum);
        request.setPageSize(pageSize);
        return request;
    }

}
